/*
 * @ author madman 
 * @ use 角色控制器业务
 */
const RolesModel = require('../../models/system/roles.model');
import categoryController from '../category/category';
const Utils = require('../utils');
class AdminRolesController {
    // 添加
    async add(ctx) {
        const { name, description, sort, power } = ctx.request.body
        const result = await RolesModel.create({ name, description, sort, power });
        if (result) {
            return ctx.success({ msg: '添加成功' });
        } else {
            return ctx.error({ msg: '添加失败' });
        }
    }

    // 更新
    async update(ctx) {
        const _body = ctx.request.body
        const _params = Utils._params(_body)
        _params.updataAt = new Date()
        const result = await RolesModel.update({ _id: _body._id }, { $set: _params });
        if (result) {
            return ctx.success({ msg: '更新成功' });
        } else {
            return ctx.error({ msg: '更新失败' });
        }
    }
    /*
     * 批量删除方法
     */
    async batchdelete(ctx, name) {
        const arrids = ctx.request.body.arrids
        let result = await RolesModel.remove({ _id: { $in: arrids } })
        if (result) {
            return ctx.success({ msg: '删除成功' });
        } else {
            return ctx.error({ msg: '删除失败' });
        }
    }
    // 删除
    async delete(ctx) {
        const _id = ctx.query.id
        const result = await RolesModel.remove({ _id: _id });
        if (result) {
            return ctx.success({ msg: '删除成功' });
        } else {
            return ctx.error({ msg: '删除失败' });
        }
    }
       // 获取角色列表
    async list(ctx) {
        const isPage = ctx.request.body.page
        const options = {
            name: 'system/roles',
            type: isPage ? 'page' : 'list',
            sortType: 'sort',
            sortVal: '1'
            // queryField: { _id: 1 }
        }
        let result = await Utils.getPageList(ctx, options)
        if (result) {
            return ctx.success({
                data: result,
                msg: '查询成功'
            });
        }
    }
}

export default new AdminRolesController;